package com.hdax.dm.mapstructs;

import com.hdax.dm.dtos.item.HotItemsDto;
import com.hdax.dm.dtos.item.IndexNavDto;
import com.hdax.dm.entity.item.DmItemType;
import com.hdax.dm.service.DmItemService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

//1. 可以使用interface
//2. 可以使用抽象类，推荐使用
@Mapper(componentModel = "spring")
public abstract class IndexNavDtoMapper {

    @Autowired
    private DmItemService dmItemService;

    @Mappings({
            @Mapping(source = "children",target = "children"),
            @Mapping(target = "hotItems",expression = "java(getHotItems(source.getId()))")
    })
    public abstract IndexNavDto toTarget(DmItemType source);
    public abstract List<IndexNavDto> toTargetList(List<DmItemType> sources);

    protected List<HotItemsDto> getHotItems(Long itemType1Id){
        return dmItemService.hotItems(itemType1Id);
    }


}
